home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue51 / Observer / FObserver_Edit.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-10-08  |  3.5 KB  |  125 lines

  1. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2.   (c) TechInsite Pty. Ltd.
  3.   PO Box 429, Abbotsford, Melbourne. 3067 Australia
  4.   Phone: +61 3 9419 6456
  5.   Fax:   +61 3 9419 1682
  6.   Web:   www.techinsite.com.au
  7.   EMail: peter_hinrichsen@techinsite.com.au
  8.  
  9.   Created: October 1999
  10.  
  11.   Notes: Edit a row of data in the subject
  12.  
  13. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  14. unit FObserver_Edit;
  15.  
  16. interface
  17.  
  18. uses
  19.   Windows, Messages, SysUtils, Classes, Graphics, Controls,
  20.   Forms, Dialogs, StdCtrls, Buttons, Mask, ComCtrls, Spin,
  21.   Subject_Portfolio, FObserver_Abstract ;
  22.  
  23. type
  24.  
  25.   //--------------------------------------------------------
  26.   TFormObserverEdit = class(TFormObserverAbstract)
  27.     btnDone: TButton;
  28.     Label4: TLabel;
  29.     Label6: TLabel;
  30.     Label7: TLabel;
  31.     eValue: TEdit;
  32.     ePrice: TEdit;
  33.     eQty: TEdit;
  34.     sbPrice: TScrollBar;
  35.     sbQty: TScrollBar;
  36.     procedure btnDoneClick(Sender: TObject);
  37.     procedure sbQtyChange(Sender: TObject);
  38.     procedure sbPriceChange(Sender: TObject);
  39.     procedure FormCreate(Sender: TObject);
  40.   private
  41.     // A pointer into the row of data to edit
  42.     FData : TStockTrans ;
  43.     procedure SetData(const Value: TStockTrans);
  44.   public
  45.     // A pointer into the row of data to edit
  46.     property  Data : TStockTrans read FData write SetData ;
  47.     // Override the vitrual abstract DataToObserver method
  48.     procedure DataToObserver ; override ;
  49.   end;
  50.  
  51. implementation
  52.  
  53. {$R *.DFM}
  54.  
  55. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  56. // *
  57. // * TFormPortfolioEdit
  58. // *
  59. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  60. procedure TFormObserverEdit.DataToObserver ;
  61. begin
  62.  
  63.   if Data = nil then
  64.     exit ; //==>
  65.  
  66.   // Set the position of the scrollBar(s)
  67.   sbQty.Position   := Data.Qty ;
  68.   sbPrice.Position := trunc( Data.Price * 100 ) ;
  69.  
  70.   // Set the text to display
  71.   eQty.Text            := floatToStr( Data.Qty   ) ;
  72.   ePrice.Text          := format( '%M', [ Data.Price ]) ;
  73.   eValue.Text          := format( '%M', [ Data.Value ]) ;
  74.  
  75. end ;
  76.  
  77. //----------------------------------------------------------
  78. procedure TFormObserverEdit.SetData(const Value: TStockTrans);
  79. begin
  80.   // Save a pointer into the data we are interested in
  81.   FData := Value;
  82.   // Set the caption of the form so we know which row of
  83.   // data we are editing
  84.   Caption := ' Edit: ' + Data.StockName + ' (' + Data.StockCode + ')' ;
  85.   // Read data from the subject into the form
  86.   DataToObserver ;
  87. end;
  88.  
  89. //----------------------------------------------------------
  90. procedure TFormObserverEdit.btnDoneClick(Sender: TObject);
  91. begin
  92.   Subject.UpdateObservers ;
  93.   Close ;
  94. end;
  95.  
  96. // The Quantity scrollBar was changed
  97. //----------------------------------------------------------
  98. procedure TFormObserverEdit.sbQtyChange(Sender: TObject);
  99. begin
  100.   // Set the new value in the subject
  101.   Data.Qty := sbQty.Position ;
  102.   // Update the observers, including this one
  103.   Subject.UpdateObservers ;
  104. end;
  105.  
  106. // The Price scrollBar was changed
  107. //----------------------------------------------------------
  108. procedure TFormObserverEdit.sbPriceChange(Sender: TObject);
  109. begin
  110.   // Set teh new value in the subject
  111.   Data.Price := sbPrice.Position / 100 ;
  112.   // Update the observers, including this one
  113.   Subject.UpdateObservers ;
  114. end;
  115.  
  116. // Assing the subject when the observer is created
  117. //----------------------------------------------------------
  118. procedure TFormObserverEdit.FormCreate(Sender: TObject);
  119. begin
  120.   inherited ;
  121.   Subject := gPortfolio ;
  122. end;
  123.  
  124. end.
  125.